﻿// -----------------------------------------------------------------------
// <copyright file="ExtensionMethods.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Cubezicles
{
    using System;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    /// <summary>
    /// Extension methods for existing classes
    /// </summary>
    public static class ExtensionMethods
    {
        // Generic version of GameServiceContainer.GetService
        public static T GetService<T>(this GameServiceContainer services) where T : class
        {
            return (T)services.GetService(typeof(T));
        }

        // Generic version of GameServiceContainer.AddService
        public static void AddService<T>(this GameServiceContainer services, T t) where T : class
        {
            services.AddService(typeof(T), t);
        }

        // Operate on all the effects of all the meshes in a given model (f is
        // a fnction that takes one argument, a BasicEffect)
        public static void ForEachEffect(this Model m, Action<BasicEffect> f)
        {
            foreach (var mesh in m.Meshes)
            {
                foreach (BasicEffect e in mesh.Effects)
                {
                    f(e);
                }
            }
        }

        // Get a Rectangle representing the default area of a sprite
        public static Rectangle GetDefaultRectangle(this Texture2D tex)
        {
            return new Rectangle(0, 0, tex.Width, tex.Height);
        }
    }
}
